package com.ywjt.platformbusiness.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.base.BaseModel;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.my.adapter.CouponAdapter;
import com.ywjt.platformbusiness.my.adapter.SignRecordAdapter;
import com.ywjt.platformbusiness.my.model.AddressModel;
import com.ywjt.platformbusiness.my.model.MyCouponListModel;
import com.ywjt.platformbusiness.my.model.SignInfoModel;
import com.ywjt.platformbusiness.util.JsonUtils;
import com.ywjt.platformbusiness.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ActivitySign extends BaseActivity implements View.OnClickListener {
    private SignRecordAdapter adapter;
    private RecyclerView rcCommon;
    private LinearLayout llSign;
    private TextView tvSignday;
    private TextView tvCurrentIntegral;
    private SignInfoModel model;
    private TextView tvSingIntegral;

    private TextView tvStatus;
    private ImageView ivBack;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivitySign.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(false);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_sign;
    }

    @Override
    protected void initView() {
        rcCommon=findViewById(R.id.rcCommon);
        ivBack=findViewById(R.id.ivBack);
        llSign=findViewById(R.id.llSign);
        tvSignday=findViewById(R.id.tvSignday);
        tvSingIntegral=findViewById(R.id.tvSingIntegral);
        tvCurrentIntegral=findViewById(R.id.tvCurrentIntegral);
        tvStatus=findViewById(R.id.tvStatus);



        ivBack.setOnClickListener(this);
        llSign.setOnClickListener(this::onClick);
        getSignInfo();
    }

    @Override
    protected void initData() {

    }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     *
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.llSign:
                //签到
                if (model.getResult().getFlag()){
                    //已经签到了
                    Toast.makeText(ActivitySign.this,"您今天已经签到完毕，请明日再来",Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    doSign();
                }
                break;
        }
    }

    //获取地址列表
    public void doSign(){
        new HttpRequest(getContext()).doPost(UrlConstants.userSignIn, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                //把请求得数据转换成json对象
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        //签到成功
                        tvStatus.setText("已签到");
                        getSignInfo();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }

    //获取签到信息
    public void getSignInfo(){
        new HttpRequest(getContext()).doPost(UrlConstants.getUserSignIn, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                //把请求得数据转换成json对象
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), SignInfoModel.class);
                        if (obj instanceof SignInfoModel) {
                              model=(SignInfoModel)obj;
                            setView();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }

    public void setView(){
        tvCurrentIntegral.setText("目前积分："+model.getResult().getNowIntegral().intValue()+"分");
        if (model.getResult().getFlag()){
            //已经签到
            tvStatus.setText("已签到");
        }else {
            //没有签到
            tvStatus.setText("签到");
        }
        tvSignday.setText("已连续签到"+model.getResult().getSignInDay().intValue()+"天");
        tvSingIntegral.setText("+"+model.getResult().getSignInNum().intValue());

        if (model.getResult().getSignInList().size()>0){
            adapter=new SignRecordAdapter(model.getResult().getSignInList(),model.getResult().getSignInNum().intValue());
            rcCommon.setLayoutManager(new LinearLayoutManager(this));
            rcCommon.setAdapter(adapter);
        }
    }
}